<?php
/**
 * 	class working with redis database using redisphp
 * 

 */

class RedisPhp{
	/**
	 * config
	 * 
	 * @var array
	 */
	protected static $_config;
   /**
    * Options
    */
    const OPT_SERIALIZER = 1;
    const OPT_PREFIX = 2;

    /**
    * Serializers
    */
    const SERIALIZER_NONE = 0;
    const SERIALIZER_PHP = 1;

	/**
	 * intances of redisPhp object
	 * 
	 * @staticvar array
	 */
	protected static $_instances = array();
	
	/**
	 * get redisPhp instance
	 * @param 	string	$connect the connect name
	 * 
	 * @return 	Redis
	 */
	 public static function getInstance($connect = null) {
         if (DEBUG){
             $rtime = microtime();
             $rtime = explode(" ",$rtime);
             $rtime = $rtime[1] + $rtime[0];
             $start_rb = $rtime;
         }
         if(($connect == null) || (!isset(CGlobal::$redis_cf[$connect]))){
            $option = CGlobal::$redis_cf['default'];
        }else{
            $option = CGlobal::$redis_cf[$connect];
            if($option == null){
                $option = CGlobal::$redis_cf['default'];
            }
        }
        self::$_config = $option['servers'];
        $redis = new Redis();
        $redis->connect(self::$_config['host'],self::$_config['port']);
        if(isset($option['db'])){
            $redis->select(self::$_config['db']);
        }
         if(DEBUG){
             $mtime = microtime();
             $mtime = explode(" ",$mtime);
             $mtime = $mtime[1] + $mtime[0];
             $end_rb = $mtime;
             $load_time = round(($end_rb - $start_rb),5)."s";
             CGlobal::$conn_debug.= ' <b>Connect to redis server : '.self::$_config['host'].' : '.self::$_config['port'].':'.self::$_config['db'].' </b> [in '.$load_time.'] <br>';
         }
        $redis->setOption(self::OPT_SERIALIZER,self::SERIALIZER_PHP);
        return  $redis;//self::$_instances[$connect];

	}


}